/******************************************************************************
 * Copyright (C) 2024 EternalChip, Inc.(Gmbh) or its affiliates.
 * 
 * All Rights Reserved.
 * 
 * @file key.c
 * 
 * @par dependencies 
 * - bsp_key.h
 * - stdio.h
 * - stdint.h
 * 
 * @author Jack | R&D Dept. | EternalChip ��оǶ��ʽ
 * 
 * @brief Provide the HAL APIs of Key and corresponding opetions.
 * 
 * Processing flow:
 * 
 * call directly.
 * 
 * @version V1.0 2024-10-30
 *
 * @note 1 tab == 4 spaces!
 * 
 *****************************************************************************/

#include "bsp_led.h"

//******************************** Declaring ********************************//
osThreadId_t led_TaskHandle;
const osThreadAttr_t led_Task_attributes = {
  .name = "led_Task",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityHigh,
};
QueueHandle_t led_queue;
/**
 * @brief Instantiates the bsp_led_handler_t target.
 * 
 * Steps:
 *  1. doing the specific operations
 *  
 * @param[in] void
 * 
 * @return led_handler_status_t : Status of the function.
 * 
 * */
led_status_t led_on_off(led_operation_t led_operation)
{
	led_status_t ret = LED_OK;
	if(LED_OK == led_operation)
	{
		HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin,GPIO_PIN_RESET);
	}
	if(LED_OFF == led_operation)
	{
		HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin,GPIO_PIN_SET);
	}
	if(LED_TOGGLE == led_operation)
	{
		HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
	}
	if(LED_BLINK_3_TIMES == led_operation)
	{
		
    for(int i = 0; i < 6;i++)
    {
        HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
        vTaskDelay(300);
    }
	}
  return ret;

}

void led_task_func(void *argument)
{
    
  led_status_t  led_ret          =          LED_OK;
  led_operation_t led_value      =          LED_ON;
  led_queue = xQueueCreate( 10, sizeof( led_operation_t ) );
  
  if (NULL == led_queue )
  {
      printf("led_queue created failed \r\n");
  } 
  else
  {
      printf("led_queue created successfully \r\n");
  }
  for(;;)
  {
    printf("Hello led thread\r\n");
    if( led_queue != 0 )
	{
		// Receive a message on the created queue.  Block for 10 ticks if a
		// message is not immediately available.
		if( pdTRUE == xQueueReceive( led_queue, &( led_value ), ( TickType_t ) 0) )
		{
			// pcRxedMessage now points to the struct AMessage variable posted
			// by vATask.
            printf("received led_queue value = [%d]  at time [%d] \r\n" , led_value,
			                                                                HAL_GetTick());
			      led_ret = led_on_off(led_value);
            if(LED_OK == led_ret)
            {
              printf("led_on_off sucessfully at time [%d]\r\n,",
							                                  HAL_GetTick());
            }
            
		}
	}
    osDelay(100);
  }
  /* USER CODE END 5 */
}
//******************************** Declaring ********************************//


